<?
require_once 'DB/DataObject.php';
/*$config = parse_ini_file('setup.ini',TRUE);
foreach($config as $class=>$values) {
    $options = &PEAR::getStaticProperty($class,'options');
    $options = $values;
}*/

define(BASE_DE_DATOS,'mysql://root:@localhost/mbmp');
define(SCHEMA_LOCATION,'/home/chulo/workspace/mbmp/db/objetos/schema');
define(CLASS_LOCATION,'/home/chulo/workspace/mbmp/db/objetos');
define(REQUIRE_PREFIX,'/home/chulo/workspace/mbmp/db/objetos');
define(CLASS_PREFIX,'DataObjects_');
define(EXTENDS_LOCATION,'DB/DataObject.php');

$options = &PEAR::getStaticProperty('DB_DataObject','options');
$options = array(
	'database'         => BASE_DE_DATOS,
	'schema_location'  => SCHEMA_LOCATION,
	'class_location'   => CLASS_LOCATION,
	'require_prefix'   => REQUIRE_PREFIX,
	'class_prefix'     => CLASS_PREFIX,
	'extends_location' => EXTENDS_LOCATION,
	'debug'            => 0
);
$opcionesAuth = array (
			'dsn'    => BASE_DE_DATOS,
			'table'       => 'usr',
			'cryptType'   => 'none',
			'usernamecol' => 'usrNombre',
			'passwordcol' => 'usrPassword'
);

require_once 'HTML/QuickForm.php';
require_once "Auth.php";
session_start();
ob_start();
$a = new Auth('DB',$opcionesAuth,null,FALSE);
$a->start();
$loginMensaje = $a->checkAuth() ?  'Logueado: ' . $a->getUsername()  : 'Ingreso:' ; 
if ($a->checkAuth() && $_POST['tracksubmit2'] == TRUE) {
 	$_SESSION['user_ok'] = TRUE;//the user has logged in	
	ob_end_flush();//used to end ob_start() at top - so header can be called after html sent 	
} 
		//if login form submitted but username incorrect	
if (!$a->checkAuth() && $_POST['tracksubmit2'] == TRUE) {
	$loginMensaje = 'Su usuario y/o contraseña son invalidos';	
}

if ($_SESSION['user_ok'] == TRUE) {//if the user is logged in then show the page else go back to index.php
	$form = new HTML_QuickForm('logOut','POST',$_SERVER['PHP_SELF'],NULL,NULL,$tracksubmit=TRUE); 
	$form->addElement('header', null, $loginMensaje);
	$form->addElement('submit','submitbtn','Salir');
	$form->addElement('hidden','tracksubmit',$tracksubmit);
	if ($_POST['tracksubmit']  == TRUE ) { 
 		$_SESSION['user_ok'] = FALSE;//user no longer logged in so set user_ok to false
		setcookie();//destroy the cookies
 		session_destroy();//destroy the session
 		header( 'Location: ' . $_SERVER['PHP_SELF'] );//go to the index page
  		ob_end_flush();//flush the output buffer	
	}
	} else {
	// if the user is not logged in then go back to index page
	$form = new HTML_QuickForm('login','POST',$_SERVER['PHP_SELF'],NULL,NULL,$tracksubmit2=TRUE);	 
	$form->addElement('hidden','tracksubmit2',$tracksubmit2);
	$form->addElement('header', null, $loginMensaje);
	$form->addElement('text','username', 'Username: ',"size=10"); 
	$form->addElement('password','password', 'Password: ',"size=10"); 
        $form->addElement('submit','submitbtn','Login');
}


?>

